using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Word = Microsoft.Office.Interop.Word;
using System.Threading;
using office = Microsoft.Office.Core;
using System.Reflection;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
private Word.Application G_wa;//定义Word应用程序字段
private object G_missing = System.Reflection.Missing.Value;//定义G_missing字段并添加引用
protected void Page_Load(object sender, EventArgs e)
{
txtOpenPath.Text = Server.MapPath("~/File/word.doc");//打开Word文档位置
txtSavePath.Text = string.Format(@"{0}\{1}", Server.MapPath("~/File"), DateTime.Now.ToString("yyyy年M月d日h时m分s秒fff毫秒") + ".doc");
}
protected void btnNew_Click(object sender, EventArgs e)
{
G_wa = new Microsoft.Office.Interop.Word.Application();//创建应用程序对象
G_wa.Visible = false;
object G_FilePath = txtOpenPath.Text;
//打开Word文档
Word.Document P_wd = G_wa.Documents.Open(
ref G_FilePath, ref G_missing, ref G_missing, ref G_missing, ref G_missing,
ref G_missing, ref G_missing, ref G_missing, ref G_missing, ref G_missing,
ref G_missing, ref G_missing, ref G_missing, ref G_missing, ref G_missing,
ref G_missing);
Word.Document P_document = G_wa.Documents.Add(ref G_missing, ref G_missing, ref G_missing, ref G_missing);//添加新的Word文档
object P_start = 0;//定义范围的开始位置
object p_end = 0;//定义范围的结束位置
Word.Range rg = P_wd.Range(ref P_start, ref p_end);//得到文档的范围
WordToWord(P_wd, P_document, rg);//将目录提取到新文档中
object P_str_path = txtSavePath.Text;//设置保存的文件名称
//保存Word文件
P_document.SaveAs(
ref P_str_path,
ref G_missing, ref G_missing, ref G_missing, ref G_missing,
ref G_missing, ref G_missing, ref G_missing, ref G_missing,
ref G_missing, ref G_missing, ref G_missing, ref G_missing,
ref G_missing, ref G_missing, ref G_missing);
object P_Save = false;//设置参数为不保存
((Word._Application)G_wa.Application).Quit(ref P_Save, ref G_missing, ref G_missing);//退出应用程序
Response.Write("<script>alert('目录已经提取完成!');</script>");
}
/// <summary>
/// 将目录提取到新文档中
/// </summary>
/// <param name="P_wd">将要提取目录的文档</param>
/// <param name="P_document">新建文档</param>
/// <param name="rg">文档范围</param>
private void WordToWord(Word.Document P_wd, Word.Document P_document, Word.Range rg)
{
object P_start = System.Reflection.Missing.Value;
object p_end = System.Reflection.Missing.Value;
object P_UseHeadingStyles = true;//是否使用内置样式创建目录
object P_UpperHeadingLevel = 1;//目录起始的标题级别
object P_LowerHeadingLevel = 9;//目录结束的标题级别
object P_UseFields = false;//是否使用TC(目录项)创建目录
object P_TableID = 1;//单字母标识符,用于根据TC域创建目录
object P_RightAlignPageNumbers = false;//目录是否右边距对齐
object P_IncludePageNumbers = false;//目录中是否包含页码
object P_AddedStyles = null;//目录其它样式的字符串名称
object P_UseHyperlinks = false;//是否将文档发布到WEB
object P_HidePageNumbersInWeb = false;//目录中的页码是否被隐藏
P_wd.TablesOfContents.Add(rg, ref P_UseHeadingStyles,//将提取的目录插入到文档的开始位置
ref P_UpperHeadingLevel, ref P_LowerHeadingLevel,
ref P_UseFields, ref P_TableID, ref P_RightAlignPageNumbers,
ref P_IncludePageNumbers, ref P_AddedStyles, ref P_UseHyperlinks,
ref P_HidePageNumbersInWeb, ref G_missing);
if (P_wd.Fields.Count >= 1)
{
P_wd.Paragraphs[1].Range.Cut();//剪切文档开始位置的目录信息
P_document.Range(ref P_start, ref p_end).Paste();//将目录信息粘贴到新文档
}
}
}
WORD:提取word文档中的目录
最新推荐文章于 2023-07-09 16:54:00 发布
本文介绍了一个使用C#编写的应用程序,该程序可以从现有的Word文档中提取目录,并将其粘贴到一个新的Word文档中,同时提供了一个简单的用户界面,包括打开路径输入框和保存路径输入框,以及用于执行操作的按钮。
摘要由CSDN通过智能技术生成